<template>
  <div class="user">
    <basic-container>
      <avue-crud
        :option="option"
        ref="crud"
        v-model="form"
        :page="page"
        @on-load="getList"
        @size-change="sizeChange"
        @current-change="currentChange"
        :table-loading="listLoading"
        @search-change="handleFilter"
        @refresh-change="handleRefreshChange"
        :before-open="handleOpenBefore"
        @search-reset="handleRefreshChange"
        @row-update="update"
        @row-save="create"
        @row-del="del"
        :data="list"
      >
        <template slot="menu" slot-scope="scope">
          <!-- <el-button
            class="filter-item"
            @click="handleUpdate(scope.row, scope.index)"
            type="text"
            icon="el-icon-edit"
            >已读
          </el-button> -->
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import { fetchList, putObj, delObj } from "@/api/admin/feedback";
import { tableOption } from "@/const/crud/admin/feedback";
import { mapGetters } from "vuex";

export default {
  name: "table_user",
  data() {
    return {
      option: tableOption,
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20, // 每页显示多少条,
        isAsc: false //是否倒序
      },
      list: [],
      listLoading: true,
      param: {},
      form: {},
    };
  },
  computed: {
    ...mapGetters(["permissions"])
  },
  created() {
  },
  methods: {
    getList(page, params) {
      this.listLoading = true;
      fetchList(
        Object.assign(
          {
            page: page.currentPage,
            size: page.pageSize
          },
          this.param
        )
      ).then(response => {
        this.list = response.data.data.data;
        this.page.total = response.data.data.count || '0';
        this.listLoading = false;
      });
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
      this.getList(this.page);
    },
    currentChange(current) {
      this.page.currentPage = current;
      this.getList(this.page);
    },
    handleFilter(param, done) {
      this.page.currentPage = 1;
      this.param = param
      if(this.param.create_time){
        this.param.start = this.param.create_time[0]
        this.param.end = this.param.create_time[1]
        delete this.param.create_time
      }
      this.getList(this.page, param);
      done();
    },
    handleRefreshChange() {
      this.getList(this.page);
    },
    handleOpenBefore(show, type) {
      show();
    },
    changeFlag(status, id) {
      let num;
      if (status === 0) {
        num = 1;
      } else {
        num = 0;
      }
      updateUser({ status: num, id: id }).then(res => {
        this.getList(this.page);
        this.$notify.success("操作成功");
      });
    },
    handleUpdate(row, index) {
      // this.$refs.crud.rowEdit(row, index);
      // this.form.password = undefined;
    },
    create(row, done, loading) {
      putObj(row)
        .then(() => {
          this.getList(this.page);
          done();
          this.$notify.success("创建成功");
        })
        .catch(() => {
          loading();
        });
    },
    update(row, index, done, loading) {
      putObj(this.form)
        .then(() => {
          this.getList(this.page);
          done();
          this.$notify.success("修改成功");
        })
        .catch(() => {
          loading();
        });
    },
    del(row){
      this.$confirm(
        "此操作将永久删除, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).then(() => {
        delObj(row.id)
          .then(() => {
            this.getList(this.page);
            this.$notify.success("操作成功");
          })
          .catch(() => {
            this.$notify.error("操作失败");
          });
      });
    },
  }
};
</script>
